<?php
namespace app\common\model;

use think\Cache;
use think\Model;

class Menu extends BaseModel
{

    /**
     * 操作菜单数据
     */
    public function operateMenu($data, $map = [])
    {
        $result = 0;

        if (!empty($data["p_id"])) {
            $p_info = $this->where(["id" => intval($data["p_id"])])->find();
            if (!empty($p_info)) {
                $data["level"] = $p_info["level"] + 1;
                $data["p_path"] = $p_info["p_path"] . $p_info["id"] . ",";
            }
        }
        if (!empty($map)) {
            $info = $this->where($map)->find();
            if (empty($info)) {
                return $result;
            }
            $data["id"] = $info["id"];
            $result = $this->save($data, $map);
        } else {
            $result = $this->save($data);
        }

        $menu_list = $this->order("sort", "desc")->select()->toArray();
        Cache::set("cache_menu", serialize($menu_list));
        return $result;

    }

    public function delMenu($id)
    {
        $map["p_path"] = array("like", "%," . $id . ",%");
        $result = $this->where('id', intval($id))->delete();
        if ($result) {
            $this->where($map)->delete();
        }
        $menu_list = $this->order("sort", "desc")->select()->toArray();
        Cache::set("cache_menu", serialize($menu_list));
        return $result;
    }

    /**
     * 通过url地址获取子级节点
     */
    public function get_child_url($url, $type = 0)
    {
      
        $returnMenu = [];
        $p_menu = $this->get_menu($url);
        $menu_list = unserialize(Cache::get("cache_menu"));
        foreach ($menu_list as $menu) {
            if ($menu["p_id"] == $p_menu["id"]&&$menu["type"]==$type) {
                $returnMenu[] = $menu;
            }
        }

        return  $returnMenu;
    }

    /**
     * 通过url获取兄弟节点
     */
    public  function get_brother_url($url, $type = 0){
        $returnMenu=[];
      $b_menu=  $this->get_menu($url);
      $menu_list = unserialize(Cache::get("cache_menu"));
      foreach ($menu_list as $menu) {
          if ($menu["p_id"] == $b_menu["p_id"]&&$menu["type"]==$type) {
              $returnMenu[] = $menu;
          }
      }
      return  $returnMenu;
    }
   /**
     * 获取当前菜单子级节点
     */
    public  function get_child_id($id,$type = 0){
        $returnMenu = [];      
        $menu_list = unserialize(Cache::get("cache_menu"));
        foreach ($menu_list as $menu) {
            if ($menu["p_id"] == $id&&$menu["type"]==$type) {
                $returnMenu[] = $menu;
            }
        }

        return  $returnMenu;
    }


     /**
     * 获取当前菜单子级节点
     */
    public  function get_brother_item($b_menu,$type = 0){
        $returnMenu=[];     
        $menu_list = unserialize(Cache::get("cache_menu"));
        foreach ($menu_list as $menu) {
            if ($menu["p_id"] == $b_menu["p_id"]&&$menu["type"]==$type) {
                $returnMenu[] = $menu;
            }
        }
        return  $returnMenu;
    }


    /**
     * 通过url 获取菜单信息
     */
    public function get_menu($url)
    {
        $menu1=[];
        $menu_list = unserialize(Cache::get("cache_menu"));
        if (empty($menu_list)) {
            $menu_list = $base_menu->all()->toArray();
            Cache::set("cache_menu", strtolower(serialize($menu_list)));
        }
   
        foreach ($menu_list as $menu) {
            if (strstr(strtolower($menu["url"]) , $url)) {
                $menu1 = $menu;
                break;
            }
        }

        return $menu1;
    }

    public  function get_menu_id($id){
        $menu1=[];
        $menu_list = unserialize(Cache::get("cache_menu"));
        if (empty($menu_list)) {
            $menu_list = $base_menu->all()->toArray();
            Cache::set("cache_menu", strtolower(serialize($menu_list)));
        }
   
        foreach ($menu_list as $menu) {
            if ($menu["id"]==$id) {
                $menu1 = $menu;
                break;
            }
        }

        return $menu1;
    }


 
}
